package com.example.fengyungo_user.activity.main;

import androidx.annotation.NonNull;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMapUtils;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.example.fengyungo_user.R;
import com.example.fengyungo_user.activity.BaseActivity;
import com.example.fengyungo_user.constant.MapConstant;
import com.example.fengyungo_user.utils.MapUtil;

/**
 * 起点到终点的路线
 *
 * @author 徐
 */
public class RouteActivity extends BaseActivity {

    private static final String TAG = "RouteActivity";

    /**
     * 地图
     */
    private MapView mapView;
    /**
     * 声明AMapLocationClient类对象
     */
    private AMapLocationClient mLocationClient = null;
    /**
     * 地图控制器
     */
    private AMap aMap = null;
    /**
     * 起点
     */
    private LatLonPoint beginLatLonPoint;
    /**
     * 起点String
     */
    private String beginPosition;
    /**
     * 终点
     */
    private LatLonPoint endLatLonPoint;
    /**
     * 终点String
     */
    private String endPosition;
    /**
     * 起点TextView
     */
    private TextView beginTv;
    /**
     * 终点TextView
     */
    private TextView endTv;
    /**
     * 定价
     */
    private TextView distanceTv;
    /**
     * 返回
     */
    private ImageButton back;
    /**
     * 与zoom相对应的比例尺3-19
     */
    private final int[] lengthForZoom = {10, 25, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000};
    /**
     * 距离：如1000m
     */
    private String distance;
    /**
     * 确认按钮
     */
    private Button okBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route);

        // 获取起终点信息
        getIntentFrom();

        back = findViewById(R.id.back);
        beginTv = findViewById(R.id.tv_begin);
        endTv = findViewById(R.id.tv_end);
        distanceTv = findViewById(R.id.tv_distance);
        okBtn = findViewById(R.id.btn_ok);

        // 设置TextView
        beginTv.setText(beginPosition);
        endTv.setText(endPosition);
        distanceTv.setText(distance);

        // 初始化地图
        initMap(savedInstanceState);

        // 搜索路线
        setBeginAndEndMarker();

        // 返回
        back.setOnClickListener((v) -> {
            finish();
        });

        // 确认
        okBtn.setOnClickListener((v) -> {
            // 跳转到添加订单信息页面
            Intent intent = new Intent(this, AddOrderActivity.class);
            // 起点
            intent.putExtra(MapConstant.BEGIN_ADDRESS, beginPosition);
            // 终点
            intent.putExtra(MapConstant.END_ADDRESS, endPosition);
            // 距离
            intent.putExtra(MapConstant.DISTANCE, distance);
            startActivity(intent);
        });
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        //销毁定位客户端，同时销毁本地定位服务。
        Log.d(TAG, "onDestroy: mLocationClient=" + mLocationClient);
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
        }
    }

    /**
     * 初始化地图
     *
     * @param savedInstanceState Bundle
     */
    private void initMap(Bundle savedInstanceState) {
        mapView = findViewById(R.id.map);
        //创建地图
        mapView.onCreate(savedInstanceState);
        //初始化地图控制器对象
        aMap = mapView.getMap();

        LatLonPoint centerLatLonPoint = new LatLonPoint(
                (beginLatLonPoint.getLatitude() + endLatLonPoint.getLatitude()) / 2,
                (beginLatLonPoint.getLongitude() + endLatLonPoint.getLongitude()) / 2);
        // 计算起点终点之间的距离，单位为米
        float distance = AMapUtils.calculateLineDistance(
                MapUtil.convertToLatLng(beginLatLonPoint), MapUtil.convertToLatLng(endLatLonPoint));
        // 根据距离设置缩放
        int zoom = 19;
        for (int length : lengthForZoom) {
            if (distance / 9 > length) {
                zoom--;
                continue;
            }
            break;
        }
        Log.d(TAG, "initMap: distance=" + distance);
        Log.d(TAG, "initMap: zoom=" + zoom);
        CameraPosition beginCameraPosition = new CameraPosition.Builder()
                .target(MapUtil.convertToLatLng(centerLatLonPoint))
                .zoom(zoom)
                .build();
        CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(beginCameraPosition);
        aMap.moveCamera(cameraUpdate);
    }

    /**
     * 获取起点、终点信息从intent
     */
    private void getIntentFrom() {
        Intent intent = getIntent();
        // 起点
        double beginLat = intent.getDoubleExtra(MapConstant.BEGIN_LAT, 0);
        double beginLon = intent.getDoubleExtra(MapConstant.BEGIN_LON, 0);
        beginLatLonPoint = new LatLonPoint(beginLat, beginLon);
        beginPosition = intent.getStringExtra(MapConstant.BEGIN_ADDRESS);
        // 终点
        double endLat = intent.getDoubleExtra(MapConstant.END_LAT, 0);
        double endLon = intent.getDoubleExtra(MapConstant.END_LON, 0);
        endLatLonPoint = new LatLonPoint(endLat, endLon);
        endPosition = intent.getStringExtra(MapConstant.END_ADDRESS);
        // 距离：例如100m
        distance = intent.getStringExtra(MapConstant.DISTANCE);
    }

    /**
     * 设置起点终点marker
     */
    private void setBeginAndEndMarker() {
        //在地图上添加起点Marker
        aMap.addMarker(new MarkerOptions()
                .position(MapUtil.convertToLatLng(beginLatLonPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.begin)));
        //在地图上添加终点Marker
        aMap.addMarker(new MarkerOptions()
                .position(MapUtil.convertToLatLng(endLatLonPoint))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.end)));
    }
}